package com.clouddms.service.transfer;

/**
 * 上传进度信息
 * 
 * @author Cloud-DMS Team
 * @version 1.0.0
 */
public class UploadProgress {
    private String uploadId;
    private int completedChunks;
    private int totalChunks;
    private long uploadedBytes;
    private long totalBytes;
    private UploadStatus status;
    private double progressPercentage;
    private long timestamp;

    public UploadProgress(String uploadId, int completedChunks, int totalChunks, 
                         long uploadedBytes, long totalBytes, UploadStatus status, double progressPercentage) {
        this.uploadId = uploadId;
        this.completedChunks = completedChunks;
        this.totalChunks = totalChunks;
        this.uploadedBytes = uploadedBytes;
        this.totalBytes = totalBytes;
        this.status = status;
        this.progressPercentage = progressPercentage;
        this.timestamp = System.currentTimeMillis();
    }

    // === 业务方法 ===

    /**
     * 获取剩余分块数
     */
    public int getRemainingChunks() {
        return totalChunks - completedChunks;
    }

    /**
     * 获取剩余字节数
     */
    public long getRemainingBytes() {
        return totalBytes - uploadedBytes;
    }

    /**
     * 检查是否已完成
     */
    public boolean isCompleted() {
        return status == UploadStatus.COMPLETED;
    }

    /**
     * 检查是否失败
     */
    public boolean isFailed() {
        return status == UploadStatus.FAILED;
    }

    /**
     * 检查是否正在进行
     */
    public boolean isActive() {
        return status.isActive();
    }

    /**
     * 格式化字节大小
     */
    public String getFormattedUploadedSize() {
        return formatBytes(uploadedBytes);
    }

    /**
     * 格式化总大小
     */
    public String getFormattedTotalSize() {
        return formatBytes(totalBytes);
    }

    /**
     * 格式化进度信息
     */
    public String getFormattedProgress() {
        return String.format("%.1f%% (%s / %s)", 
            progressPercentage, getFormattedUploadedSize(), getFormattedTotalSize());
    }

    private String formatBytes(long bytes) {
        if (bytes < 1024) return bytes + " B";
        int exp = (int) (Math.log(bytes) / Math.log(1024));
        String pre = "KMGTPE".charAt(exp - 1) + "";
        return String.format("%.1f %sB", bytes / Math.pow(1024, exp), pre);
    }

    // === Getters ===

    public String getUploadId() {
        return uploadId;
    }

    public int getCompletedChunks() {
        return completedChunks;
    }

    public int getTotalChunks() {
        return totalChunks;
    }

    public long getUploadedBytes() {
        return uploadedBytes;
    }

    public long getTotalBytes() {
        return totalBytes;
    }

    public UploadStatus getStatus() {
        return status;
    }

    public double getProgressPercentage() {
        return progressPercentage;
    }

    public long getTimestamp() {
        return timestamp;
    }

    @Override
    public String toString() {
        return String.format("UploadProgress{uploadId='%s', progress=%.1f%%, status=%s, chunks=%d/%d}", 
            uploadId, progressPercentage, status, completedChunks, totalChunks);
    }
}